home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / VISCAFE.BIN / DbaDataStore.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-06-19  |  9.9 KB  |  508 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import java.util.BitSet;
  4. import symantec.itools.db.pro.MultiView;
  5. import symantec.itools.db.pro.RelationView;
  6. import symantec.itools.db.pro.RelationViewMetaData;
  7. import symjava.sql.SQLException;
  8.  
  9. public class DbaDataStore implements DbDataStore, MetaTable, DbDataUpdater {
  10.    Position position;
  11.    RelationView master;
  12.    RelationView relView;
  13.    MultiView multiView;
  14.    DbDataSource source;
  15.    DbaDataLink link;
  16.    int coltoshow;
  17.    RelationViewMetaData meta;
  18.    public static final int MAX_DISPLAY_SIZE = 60;
  19.    int[] rowMapping;
  20.    BitSet createdRows;
  21.    int lastValidIndex;
  22.    int lastIndex_;
  23.    int lastRowRetrieved;
  24.    boolean obtainedAllRows = false;
  25.    static final int START_SIZE = 100;
  26.    static final int INC_SIZE = 100;
  27.  
  28.    public DbaDataStore(RelationView var1, RelationView var2) throws SQLException {
  29.       if (var2 != null) {
  30.          this.master = var2;
  31.       }
  32.  
  33.       this.relView = var1;
  34.       this.multiView = var1.getMultiView();
  35.       this.meta = this.relView.getMetaData();
  36.       this.position = new Position(var1);
  37.    }
  38.  
  39.    void resetRows() throws SQLException {
  40.       int var1 = this.position.getIgnoreCount();
  41.       if (var1 <= 0) {
  42.          this.initRowMapping();
  43.          this.source.view.clear();
  44.       }
  45.    }
  46.  
  47.    void notifyRecordChange() throws SQLException {
  48.       int var1 = this.position.getIgnoreCount();
  49.       if (var1 <= 0) {
  50.          int var2 = this.position.get();
  51.          if (this.relView.getCurrentRecordState() == 102) {
  52.             this.source.view.scrollUpDownAbsolute(var2 - 1);
  53.          }
  54.  
  55.          this.source.view.setFocusToRow(var2);
  56.       }
  57.    }
  58.  
  59.    public void fetchMode(boolean var1) {
  60.       this.setManRowChangeFlag(var1);
  61.    }
  62.  
  63.    public boolean manualRowChangeFlag() {
  64.       return this.position.getNotificationMode();
  65.    }
  66.  
  67.    private void setManRowChangeFlag(boolean var1) {
  68.       try {
  69.          this.position.setNotificationMode(var1);
  70.       } catch (SQLException var2) {
  71.       }
  72.    }
  73.  
  74.    public void setCurrentRow(int var1) throws TypeNotSupported {
  75.       ++var1;
  76.  
  77.       try {
  78.          int var2 = this.translateRow(var1);
  79.          this.position.set(var2);
  80.       } catch (Exception var3) {
  81.          throw new TypeNotSupported(((Throwable)var3).getMessage());
  82.       }
  83.    }
  84.  
  85.    public boolean isCurrentRow(int var1) {
  86.       try {
  87.          int var2 = this.translateRow(var1);
  88.          return var2 == this.position.get();
  89.       } catch (Exception var3) {
  90.          return false;
  91.       }
  92.    }
  93.  
  94.    public void setDbDataSource(DbDataSource var1) {
  95.       if (this.source == null) {
  96.          this.source = var1;
  97.          this.initRowMapping();
  98.          this.link = new DbaDataLink(this);
  99.          this.position.ignoreNotification(true);
  100.  
  101.          try {
  102.             this.relView.bindRecordSet(this.link);
  103.             this.relView.bindCurrentRecord(this.link);
  104.          } catch (SQLException var2) {
  105.          }
  106.  
  107.          this.position.ignoreNotification(false);
  108.       }
  109.    }
  110.  
  111.    public boolean supportsCaching() {
  112.       return true;
  113.    }
  114.  
  115.    public int validDataRowRange(int var1, int var2) throws DataNotAvailable {
  116.       ++var1;
  117.       var2 += 2;
  118.       int var3 = Math.max(1, var1);
  119.  
  120.       try {
  121.          this.setManRowChangeFlag(true);
  122.  
  123.          while(var3 <= var2) {
  124.             this.translateRow(var3);
  125.             ++var3;
  126.          }
  127.       } catch (DataNotAvailable var8) {
  128.       } finally {
  129.          this.setManRowChangeFlag(false);
  130.       }
  131.  
  132.       if (var3 == var1) {
  133.          throw new DataNotAvailable("top is greater than last row in database");
  134.       } else {
  135.          return var3 - 1;
  136.       }
  137.    }
  138.  
  139.    public Data getData(int var1, int var2) throws DataNotAvailable {
  140.       ++var1;
  141.       Object var3 = null;
  142.  
  143.       try {
  144.          this.setManRowChangeFlag(true);
  145.          int var6 = this.translateRow(var1);
  146.          boolean var7 = this.position.set(var6);
  147.          if (!var7) {
  148.             throw new DataNotAvailable("Could not move to row " + var1);
  149.          }
  150.  
  151.          String var8 = this.relView.getStringValue(var2);
  152.          if (var8 == null) {
  153.             var8 = "";
  154.          }
  155.  
  156.          var14 = new ImageStringData(this.source, var8);
  157.       } catch (Exception var11) {
  158.          throw new DataNotAvailable(((Throwable)var11).getMessage());
  159.       } finally {
  160.          this.setManRowChangeFlag(false);
  161.       }
  162.  
  163.       return var14;
  164.    }
  165.  
  166.    public void update(int var1, int var2, Data var3) throws TypeNotSupported {
  167.       ++var1;
  168.  
  169.       try {
  170.          this.setManRowChangeFlag(true);
  171.          int var6 = this.translateRow(var1);
  172.          this.position.set(var6);
  173.          this.relView.setValueFromString(var2, var3.toString());
  174.       } catch (Exception var9) {
  175.          throw new TypeNotSupported(((Throwable)var9).getMessage());
  176.       } finally {
  177.          this.setManRowChangeFlag(false);
  178.       }
  179.  
  180.    }
  181.  
  182.    public int rowState(int var1) {
  183.       ++var1;
  184.       byte var2 = 0;
  185.  
  186.       try {
  187.          this.setManRowChangeFlag(true);
  188.          int var5 = this.translateRow(var1);
  189.          this.position.set(var5);
  190.          byte var6 = this.relView.getCurrentRecordState();
  191.          switch (var6) {
  192.             case 100:
  193.                var2 = 1;
  194.                break;
  195.             case 101:
  196.                var2 = 3;
  197.                break;
  198.             case 102:
  199.                var2 = 0;
  200.                break;
  201.             case 103:
  202.                var2 = 2;
  203.          }
  204.       } catch (Exception var9) {
  205.          ((Throwable)var9).printStackTrace();
  206.       } finally {
  207.          this.setManRowChangeFlag(false);
  208.       }
  209.  
  210.       return var2;
  211.    }
  212.  
  213.    public void clear() {
  214.       this.initRowMapping();
  215.    }
  216.  
  217.    public void refresh() {
  218.       try {
  219.          this.relView.restartMultiView();
  220.       } catch (SQLException var1) {
  221.       }
  222.    }
  223.  
  224.    public void undoRow(int var1) throws TypeNotSupported {
  225.       ++var1;
  226.  
  227.       try {
  228.          this.position.ignoreNotification(true);
  229.          this.relView.undoRecord();
  230.       } catch (SQLException var7) {
  231.          throw new TypeNotSupported(((Throwable)var7).getMessage());
  232.       } finally {
  233.          this.position.ignoreNotification(false);
  234.       }
  235.  
  236.    }
  237.  
  238.    public int rowsRetrieved() {
  239.       return this.lastIndex_;
  240.    }
  241.  
  242.    public int fetchAllRows() {
  243.       int var1 = 1;
  244.  
  245.       try {
  246.          this.setManRowChangeFlag(true);
  247.  
  248.          while(true) {
  249.             this.translateRow(var1);
  250.             ++var1;
  251.          }
  252.       } catch (DataNotAvailable var6) {
  253.       } finally {
  254.          this.setManRowChangeFlag(false);
  255.       }
  256.  
  257.       return var1 - 1;
  258.    }
  259.  
  260.    public void undeleteRow(int var1) throws TypeNotSupported {
  261.       try {
  262.          ++var1;
  263.          this.position.ignoreNotification(true);
  264.          this.relView.undoRecord();
  265.       } catch (SQLException var7) {
  266.          throw new TypeNotSupported(((Throwable)var7).getMessage());
  267.       } finally {
  268.          this.position.ignoreNotification(false);
  269.       }
  270.  
  271.    }
  272.  
  273.    public void deleteRow(int var1) throws TypeNotSupported {
  274.       try {
  275.          ++var1;
  276.          this.position.ignoreNotification(true);
  277.          this.relView.deleteRecord();
  278.       } catch (SQLException var7) {
  279.          throw new TypeNotSupported(((Throwable)var7).getMessage());
  280.       } finally {
  281.          this.position.ignoreNotification(false);
  282.       }
  283.  
  284.    }
  285.  
  286.    public void save() throws TypeNotSupported {
  287.       try {
  288.          this.relView.saveMultiView();
  289.       } catch (SQLException var2) {
  290.          throw new TypeNotSupported(((Throwable)var2).getMessage());
  291.       }
  292.    }
  293.  
  294.    public void getNewRecord() throws TypeNotSupported {
  295.       try {
  296.          this.relView.getNewRecord();
  297.       } catch (SQLException var2) {
  298.          throw new TypeNotSupported(((Throwable)var2).getMessage());
  299.       }
  300.    }
  301.  
  302.    public void insertRow(int var1) throws TypeNotSupported {
  303.       this.appendRow();
  304.    }
  305.  
  306.    public int appendRow() throws TypeNotSupported {
  307.       this.getNewRecord();
  308.       return 0;
  309.    }
  310.  
  311.    public void setColtoShow(int var1) {
  312.       this.coltoshow = var1;
  313.    }
  314.  
  315.    public void setupGrid(Grid var1) {
  316.       try {
  317.          var1.setAutoRedraw(false);
  318.          int var2 = this.coltoshow;
  319.          if (this.coltoshow == 0 || this.meta.getColumnCount() <= this.coltoshow) {
  320.             var2 = this.meta.getColumnCount();
  321.          }
  322.  
  323.          var1.createColumns(var2);
  324.          var1.setRowLabelHeadingStyle(0);
  325.  
  326.          for(int var3 = 1; var3 <= var2; ++var3) {
  327.             int var4 = this.meta.getColumnDisplaySize(var3);
  328.             var4 = Math.min(0, var4);
  329.             var4 = Math.max(0, 60);
  330.             var1.setHeading(this.meta.getColumnLabel(var3), var3, Math.min(var4, 10));
  331.             this.setupColumn(var1, var3);
  332.          }
  333.  
  334.          var1.setAutoRedraw(true);
  335.       } catch (SQLException var5) {
  336.       }
  337.    }
  338.  
  339.    public void setDataSource(DataSource var1) {
  340.    }
  341.  
  342.    public boolean isDataEditable(int var1, int var2) throws DataNotAvailable {
  343.       boolean var7;
  344.       try {
  345.          ++var1;
  346.          this.setManRowChangeFlag(true);
  347.          int var5 = this.translateRow(var1);
  348.          boolean var6 = this.position.set(var5);
  349.          if (!var6) {
  350.             return true;
  351.          }
  352.  
  353.          var7 = this.meta.isWritable(var2) && this.relView.isCurrentRecordWritable();
  354.       } catch (SQLException var10) {
  355.          throw new DataNotAvailable(((Throwable)var10).getMessage());
  356.       } finally {
  357.          this.setManRowChangeFlag(false);
  358.       }
  359.  
  360.       return var7;
  361.    }
  362.  
  363.    public Data[] arrangeForViewing(Data[] var1) {
  364.       return var1;
  365.    }
  366.  
  367.    public void setupColumn(Grid var1, int var2) {
  368.       try {
  369.          int var3 = this.meta.getColumnType(var2);
  370.          switch (var3) {
  371.             case -7:
  372.             case -4:
  373.             case -3:
  374.             case -2:
  375.             case -1:
  376.             case 0:
  377.             case 1:
  378.             case 12:
  379.             case 91:
  380.             case 92:
  381.             case 93:
  382.             case 1111:
  383.             default:
  384.                var1.setColumnAlignment(var2, 0);
  385.                break;
  386.             case -6:
  387.             case -5:
  388.             case 2:
  389.             case 4:
  390.             case 5:
  391.             case 6:
  392.             case 7:
  393.             case 8:
  394.                var1.setColumnAlignment(var2, 2);
  395.          }
  396.  
  397.          if (this.meta.isCurrency(var2)) {
  398.             var1.setColumnAlignment(var2, 2);
  399.          }
  400.  
  401.          if (!this.meta.isWritable(var2)) {
  402.             var1.setColEditable(var2, false);
  403.             return;
  404.          }
  405.       } catch (SQLException var4) {
  406.          var1.setColumnAlignment(var2, 0);
  407.       }
  408.  
  409.    }
  410.  
  411.    private void initRowMapping() {
  412.       this.rowMapping = new int[100];
  413.       this.lastValidIndex = 99;
  414.       this.lastRowRetrieved = 0;
  415.       this.lastIndex_ = 0;
  416.       this.obtainedAllRows = false;
  417.       this.createdRows = new BitSet();
  418.    }
  419.  
  420.    void printMap() {
  421.       System.out.println("Row mappings - size=" + this.lastValidIndex + "\n\tlastIndex=" + this.lastIndex_ + "  lastRow=" + this.lastRowRetrieved);
  422.  
  423.       for(int var1 = 0; var1 <= this.lastIndex_; ++var1) {
  424.          System.out.println("\trow " + var1 + " -> " + this.rowMapping[var1]);
  425.       }
  426.  
  427.    }
  428.  
  429.    private int translateRow(int var1) throws DataNotAvailable {
  430.       try {
  431.          while(this.lastIndex_ < var1) {
  432.             if (this.createdRows.get(this.lastRowRetrieved + 1)) {
  433.                ++this.lastRowRetrieved;
  434.             } else {
  435.                boolean var2 = this.position.set(this.lastRowRetrieved + 1);
  436.                if (!var2) {
  437.                   this.obtainedAllRows = true;
  438.                   throw new DataNotAvailable("Ran out of rows to fetch at row=" + (this.lastRowRetrieved + 1));
  439.                }
  440.  
  441.                this.lastRowRetrieved = this.relView.getCurrentRecordNumber();
  442.                ++this.lastIndex_;
  443.                this.growMap();
  444.                this.rowMapping[this.lastIndex_] = this.lastRowRetrieved;
  445.             }
  446.          }
  447.  
  448.          return this.rowMapping[var1];
  449.       } catch (SQLException var4) {
  450.          throw new DataNotAvailable(((Throwable)var4).getMessage());
  451.       }
  452.    }
  453.  
  454.    private int insertRowIntoMap(int var1) throws DataNotAvailable {
  455.       try {
  456.          this.translateRow(var1);
  457.          this.relView.getNewRecord();
  458.          int var2 = this.relView.getCurrentRecordNumber();
  459.          this.createdRows.set(var2);
  460.          this.growMap();
  461.          int var3 = this.lastIndex_ + 1 - var1;
  462.          System.arraycopy(this.rowMapping, var1, this.rowMapping, var1 + 1, var3);
  463.          ++this.lastIndex_;
  464.          this.rowMapping[var1] = var2;
  465.          return var2;
  466.       } catch (Exception var4) {
  467.          throw new DataNotAvailable(((Throwable)var4).getMessage());
  468.       }
  469.    }
  470.  
  471.    private int appendRowIntoMap() throws DataNotAvailable {
  472.       try {
  473.          while(!this.obtainedAllRows) {
  474.             this.translateRow(this.lastRowRetrieved + 1);
  475.          }
  476.       } catch (DataNotAvailable var3) {
  477.          if (!this.obtainedAllRows) {
  478.             throw var3;
  479.          }
  480.       }
  481.  
  482.       try {
  483.          this.relView.getNewRecord();
  484.          this.growMap();
  485.          this.lastRowRetrieved = this.relView.getCurrentRecordNumber();
  486.          this.createdRows.set(this.lastRowRetrieved);
  487.          this.rowMapping[++this.lastIndex_] = this.lastRowRetrieved;
  488.       } catch (SQLException var2) {
  489.          throw new DataNotAvailable("Could not create new record");
  490.       }
  491.  
  492.       return this.lastIndex_;
  493.    }
  494.  
  495.    final void growMap() {
  496.       if (this.lastIndex_ == this.lastValidIndex - 1) {
  497.          int[] var1 = new int[this.lastValidIndex += 100];
  498.          System.arraycopy(this.rowMapping, 0, var1, 0, this.rowMapping.length);
  499.          this.rowMapping = var1;
  500.       }
  501.  
  502.    }
  503.  
  504.    public Object getSynchronizationObject() {
  505.       return this.multiView;
  506.    }
  507. }
  508.